#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "rx203_ptcl_parsor.h"

#include <QFileDialog>

//RefreshTask::RefreshTask(UdpReceivor* udp_receivor, ParsorBase* parsor,
//                         QObject *parent) :
//    udp_receivor(udp_receivor),
//    parsor(parsor),
//    QObject(parent)
//{
//    qDebug("[%s]  new", __func__);


//}

//RefreshTask::~RefreshTask()
//{
//    taskStop();
//    qDebug("[%s]  delete", __func__);
//}

//bool RefreshTask::taskIsRun()
//{
//    return flag_run;
//}

//void RefreshTask::slot_refreshTaskInit(void)
//{
//    qDebug("[%s] thread tid(%lu)", __func__, QThread::currentThreadId());

//    connect(udp_receivor, SIGNAL(signal_getUdpDataReady()),
//            this, SLOT(slot_getRawData()));

//    udp_receivor->startRecv();
////    while (flag_run) {

////    }
//    return;
//}

//void RefreshTask::slot_getRawData()
//{
//    qDebug("[%s] thread tid(%lu)", __func__, QThread::currentThreadId());
//    QByteArray* raw_data = new QByteArray(*udp_receivor->getRecvBuffer());
//    parsor->addRawPkt(*raw_data);
//}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    display_window(NULL),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->le_w->setValidator(new QIntValidator(1, 1920, this));
    ui->le_h->setValidator(new QIntValidator(1, 1080, this));

    ui->le_ip_1->setValidator(new QIntValidator(0, 255, this));
    ui->le_ip_2->setValidator(new QIntValidator(0, 255, this));
    ui->le_ip_3->setValidator(new QIntValidator(0, 255, this));
    ui->le_ip_4->setValidator(new QIntValidator(0, 255, this));

    ui->le_port->setValidator(new QIntValidator(0, 65535, this));

    ui->le_thread_num->setValidator(new QIntValidator(2, 8, this));

    qDebug("[%s] thread tid(%lu)", __func__, QThread::currentThreadId());







    connect(ui->btn_sample, SIGNAL(clicked(bool)),
            this, SLOT(slot_click_btn_sample()));

    connect(ui->btn_stop, SIGNAL(clicked(bool)),
            this, SLOT(slot_click_btn_stop()));



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_click_btn_sample()
{
    qDebug("[%s] entry", __func__);
    if (ui->le_ip_1->text().isEmpty() || ui->le_ip_2->text().isEmpty() ||
            ui->le_ip_3->text().isEmpty() || ui->le_ip_4->text().isEmpty() ||
            ui->le_port->text().isEmpty() || ui->le_par_file->text().isEmpty() ||
            ui->le_thread_num->text().isEmpty()) {
        qErrnoWarning("[%s]need complete info!", __func__);

        return;
    }

    // 创建解析器
    try {
        parsor = new RX203_PTCL_Parsor(ui->le_par_file->text(),
                                       ui->le_thread_num->text().toInt());
    } catch (RX203_ERR_CODE code) {
        if (code != RX203_ERR_CODE_OK) {
            qErrnoWarning("new Parsor ERR! err code:%d", code);
            parsor = NULL;
            return;
        }
    }

    //创建 udp 客户端
    QString ip = ui->le_ip_1->text() + "." + ui->le_ip_2->text() +
            "." + ui->le_ip_3->text() + "." + ui->le_ip_4->text();

    udp_receivor = new UdpReceivor(ip, ui->le_port->text().toInt());

    connect(udp_receivor, SIGNAL(signal_getUdpDataReady()),
            this, SLOT(slot_getRawData()));


    //创建播放窗口
    display_window = new Wgt_Display(parsor->getCompeletImage(), NULL);

    connect(display_window, SIGNAL(destroyed(QObject*)),
            this, SLOT(slot_display_window_close()));


    connect(parsor, SIGNAL(signal_frameready()),
            display_window, SLOT(repaint()));

    display_window->show();
    udp_receivor->startRecv();
}

void MainWindow::slot_getRawData()
{
    parsor->addRawPkt(udp_receivor->getRecvBuffer());
}

void MainWindow::slot_click_btn_stop()
{
    qDebug("[%s] entry", __func__);
    if (display_window != NULL) {
        //display_window->stop_refresh();
        display_window->close();

        display_window = NULL;
    }
}

void MainWindow::slot_display_window_close()
{
    qDebug("[%s] entry", __func__);
    if (udp_receivor) {
        delete udp_receivor;
        udp_receivor = NULL;
    }
    if (parsor) {

    }
    display_window = NULL;
}
